package com.app.controller;

import com.app.common.Result;
import com.app.model.AdminUserModel;
import com.app.service.AdminHomepageService;
import com.app.service.AdminUserService;
import com.app.utils.TokenUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;

@Slf4j
@RestController
@Controller
@RequestMapping("/homepage")
public class AdminHomepageController {
    @Autowired
    private AdminHomepageService adminHomepageService;
    @Autowired
    private AdminUserService adminUserService;

    /**
     * 近一周用户益联券消费情况 左下
     *
     * @return
     */
    @GetMapping("/voucherConsumption")
    public Result<Object> voucherConsumption(@RequestParam(required = false) Timestamp beginTime,
                                             @RequestParam(required = false) Timestamp endTime,
                                             HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return adminHomepageService.voucherConsumption(beginTime, endTime,user);
    }

    /**
     * 近三十天推广情况分析  左上
     *
     * @return
     */
    @GetMapping("/promotion")
    public Result<Object> Promotion(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return adminHomepageService.Promotion(user);
    }

    /**
     * 商家们进三十天益联券的总收入情况、要是传了商家id那就是当前商家进三十天的益联券收入情况  右上
     *
     * @param id
     * @return
     */
    @GetMapping("/voucherIncome")
    public Result<Object> voucherIncome(@RequestParam(required = false) Integer id,HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return adminHomepageService.voucherIncome(id,user);
    }

    /**
     * 商家销量top5   右下
     *
     * @return
     */
    @GetMapping("/top10")
    public Result<Object> top10(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return adminHomepageService.top10(user);
    }

    /**
     * 财务报表
     */
    @PostMapping("/financialStatements")
    public Result financialStatements( HttpServletRequest request,
                                      @RequestBody Params params) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
    return adminHomepageService.financialStatements(user,params.getStartTime(),params.getEndTime(),params.getAreaId());
    }


    /**
     * 财务报表
     *
     * @param request 请求
     * @param params  params
     * @return {@link Result }
     */
    @PostMapping("/consumerData")
    public Result consumerData( HttpServletRequest request,
                                       @RequestBody Params params) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.consumerData(user,params.getStartTime(),params.getEndTime(),params.getAreaId()));
    }


    /**
     * 转账数据
     *
     * @param request 请求
     * @param params  params
     * @return {@link Result }
     */
    @PostMapping("/transferData")
    public Result transferData(HttpServletRequest request,
                                @RequestBody Params params) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.transferData(user,params.getStartTime(),params.getEndTime(),params.getAreaId()));
    }


    /**
     * 传输数据列表
     *
     * @param request 请求
     * @param params  params
     * @return {@link Result }
     */
    @PostMapping("/transferDataList")
    public Result transferDataList(HttpServletRequest request,
                               @RequestBody Params params) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.transferDataList(params.getType(),params.getPage(),params.getLimit(),user,params.getStartTime(),params.getEndTime(),params.getAreaId()));
    }


    /**
     * 充值金额列表
     * 传输数据列表
     *
     * @param request 请求
     * @param params  params
     * @return {@link Result}
     */
    @PostMapping("/rechargeAmountList")
    public Result rechargeAmountList(HttpServletRequest request,
                                   @RequestBody Params params) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.rechargeAmountList(params.getMobile(),params.getPage(),params.getLimit(),user,params.getStartTime(),params.getEndTime()));
    }


    /**
     * 消费者数据列表
     *
     * @param request 请求
     * @param params  params
     * @return {@link Result }
     */
    @PostMapping("/consumerDataList")
    public Result consumerDataList( HttpServletRequest request,
                                @RequestBody Params params) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.consumerDataList(params.getPage(),params.getLimit(),user,params.getStartTime(),params.getEndTime(),params.getAreaId(),params.getType()));
    }



    /**
     * 凭证详细信息合计
     *
     * @param request 请求
     * @param params  params
     * @return {@link Result}
     */
    @PostMapping("/totalVoucherDetails")
    public Result totalVoucherDetails( HttpServletRequest request,
                                       @RequestBody Params params) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.totalVoucherDetails(user,
                params.getStartTime(),
                params.getEndTime(),
                params.getAreaId(),
                params.getPage(),
                params.getLimit()));
    }

    /**
     * 凭证详细信息合计
     *
     * @param request 请求
     * @param params  params
     * @return {@link Result}
     */
    @PostMapping("/totalPointsDetails")
    public Result totalPointsDetails( HttpServletRequest request,
                                       @RequestBody Params params) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.totalPointsDetails(user,
                params.getStartTime(),
                params.getEndTime(),
                params.getAreaId(),
                params.getPage(),
                params.getLimit()));
    }


    @PostMapping("/totalPumpingDetails")
    public Result totalPumpingDetails( HttpServletRequest request,
                                      @RequestBody Params params) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.totalPumpingDetails(user,
                params.getStartTime(),
                params.getEndTime(),
                params.getAreaId(),
                params.getPage(),
                params.getLimit()));
    }



    @PostMapping("/memberOpeningAmount")
    public Result memberOpeningAmount( HttpServletRequest request,
                                       @RequestBody Params params) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.memberOpeningAmount(user,
                params.getStartTime(),
                params.getEndTime(),
                params.getAreaId(),
                params.getPage(),
                params.getLimit()));
    }


    /**
     * 总循环量
     *
     * @param request 请求
     * @param params  params
     * @return {@link Result}
     */
    @PostMapping("/totalCirculation")
    public Result totalCirculation( HttpServletRequest request,
                                       @RequestBody Params params) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.totalCirculation(user,
                params.getStartTime(),
                params.getEndTime(),
                params.getAreaId(),
                params.getPage(),
                params.getLimit()));
    }

    /**
     * 抽层流水走势
     *
     * @return
     */
    @GetMapping("/businessTrendsApi")
    public Result businessTurnover(@RequestParam(defaultValue = "1") Integer type, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.businessTurnover(user, type));
    }

    /**
     * 营业额走势
     */
    @GetMapping("/turnoverTrend")
    public Result turnoverTrend(@RequestParam(defaultValue = "1") Integer type, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.turnoverTrend(user, type));
    }

    /**
     * 商品订单统计数据
     * @param request
     * @return
     */
    @GetMapping("/goodsOrderNews")
    public Result goodsOrderNews(HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.goodsOrdernews(user));
    }

    /**
     * 活动订单统计数据
     * @param request
     * @return
     */
    @GetMapping("/activityOrderNews")
    public Result activityOrderNews(HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.activityOrderNews(user));
    }

    /**
     * VIP统计数据
     * @param request
     * @return
     */
    @GetMapping("/vipNews")
    public Result vipNews(HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.vipNews(user));
    }

    /**
     * 用户统计数据
     * @param request
     * @param isVip 1vip  2粉丝
     * @return
     */
    @GetMapping("/userNews")
    public Result userNews(Integer isVip, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(adminHomepageService.userNews(user, isVip));
    }




}

@Data
class Params {
    private String startTime;
    private String endTime;
    private Integer areaId;
    private Integer page;
    private Integer limit;
    private Integer type;
    private String mobile;
}
